import { Button, Modal } from 'antd';
import React, { forwardRef, useState } from 'react';
type Props = {
  children: React.ReactNode
  title?: string
  width?:number
  handleOk?: (type: string) => void
  autoClose?: boolean
}
const Modals2 = forwardRef((props: Props, onref) => {
  const { title = '默认标题', autoClose = true,width=500 } = props
  // 暴露给父组件的属性
  const currentObj = {
    showModal,
    handleOk,
    handleCancel,
    hide,
    Modal
  }
  // @ts-ignore
  onref.current = currentObj
  const [isModalOpen, setIsModalOpen] = useState(false);
  function showModal() {
    setIsModalOpen(true);
  };

  function handleOk(e) {
    if (props.handleOk) {
      props.handleOk('config')

    }
    if (autoClose) {
      hide()

    }
  };

  function handleCancel() {
    if (props.handleOk) {
      props.handleOk('cancel')

    }
    if (autoClose) {
      hide()
    }
  };
  function hide() {

    setIsModalOpen(false);
  }
  return (
    <Modal width={width} title={title} open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
      {props.children}
    </Modal>
  );
})

export default Modals2;